package com.patternbox.eclipse.model;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;

import com.patternbox.eclipse.views.TreeFile;

public class ScenarioInstancePool {
	
	public static final String SCENARIOS_FOLDER = "Scenarios";
	public static final String SCENARIOS_EXT = "sce";
	
	private static ArrayList<ScenarioInstance> sScenarios;
	
	public static void initialize() {
		sScenarios = new ArrayList<ScenarioInstance>();
		try {

			// Filtra todos los .xdp del workspace
			IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace()
					.getRoot();
			IProject[] projects = workspaceRoot.getProjects();
			ArrayList<IFolder> folders = new ArrayList<IFolder>();
			for (IProject p : projects) {
				IFolder f = p.getFolder(SCENARIOS_FOLDER);
				if (f != null)
					folders.add(f);
			}
			ArrayList<IFile> workspaceFiles = new ArrayList<IFile>();
			for (IFolder f : folders) {
				if (f.exists()) {
					for (IResource r : f.members()) {
						if (r.getType() == IResource.FILE
								&& r.getFileExtension().equals(
										SCENARIOS_EXT)) {
							workspaceFiles.add((IFile) r);
						}
					}
				}
			}

			for (IFile f : workspaceFiles) {
				sScenarios.add(new ScenarioInstance(f));
			}
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	public static ArrayList<ScenarioInstance> getInstances() {
		return sScenarios;
	}

}
